package mundomensajero.main;

import java.util.ArrayList;

import mundomensajero.data.FavoritesSQLiteHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class RecientesView extends Activity {
		ListView lv;
		ArrayList<DestinatarioData> data = new ArrayList<DestinatarioData>();
		int TIGO=R.drawable.tigo_logo;
	    int CLARO=R.drawable.claro_logo;
	    int DIGICEL=R.drawable.digicel_logo;
	    int MOVISTAR=R.drawable.movistar_logo;
	
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main_viewrecientes);
	        lv = (ListView)findViewById(R.id.LstOpciones);
	        
	        sacarData();

	        AdaptadorDestinatarios adaptador = new AdaptadorDestinatarios(this,data.size());
	        lv.setAdapter(adaptador);
	        
	        lv.setOnItemClickListener(new OnItemClickListener() {
	            public void onItemClick(AdapterView<?> parent, View v, int posicion, long id) {
	                sendResult(lv.getItemIdAtPosition(posicion));
	            }
	        }); 
	    }
		
		public void sendResult(Object obj){
	    	Long dd =  (Long) obj;
	    	Intent principal = new Intent();
			principal.setClass(this,MundoMensajero_Activity.class);
			principal.putExtra("nombre", data.get(Integer.parseInt(dd+"")).getNombre());
			principal.putExtra("numero", data.get(Integer.parseInt(dd+"")).getNumero());
			setResult(RESULT_OK, principal);
			finish();
	    }
		
		public void onClickBorrar(View Button){
			if(data.size()==0) return;
			try{
				FavoritesSQLiteHelper usdbh = new FavoritesSQLiteHelper(this, "DBFavorites_MM", null, 1);
				SQLiteDatabase db = usdbh.getWritableDatabase();
				db.delete("Recientes",null, null);
				db.close();
				data.clear();
				AdaptadorDestinatarios adaptador = new AdaptadorDestinatarios(this,data.size());
				lv.setAdapter(adaptador);
			}catch(Exception e){Log.i("ERROR DB", e.getMessage());}
		}
		
	    public void onClickRegresar(View Button){
	    	Intent principal = new Intent();
			principal.setClass(this,MundoMensajero_Activity.class);
			setResult(RESULT_CANCELED, principal);
			finish();
	    }
		
		 private void sacarData(){
			FavoritesSQLiteHelper usdbh = new FavoritesSQLiteHelper(this, "DBFavorites_MM", null, 1);
	    	SQLiteDatabase db = usdbh.getWritableDatabase();
	    	String[] campos = new String[] {"nombre","numero"};
	    	Cursor c = db.query("Recientes", campos, null, null, null, null, "nombre ASC");
	    	if (c.moveToFirst()) {
	   	     	do {
	   	     		data.add(new DestinatarioData(c.getString(0),c.getString(1), true));//Guardamos todos los recientes que estan almacenados en la BD
	   	     	} while(c.moveToNext());
	    	}
		 }
		 
		 
		 private class AdaptadorDestinatarios extends BaseAdapter {
		    	Activity context;

		    	AdaptadorDestinatarios(Activity context,int size) {
		    		super();
		    		this.context = context;
		    	}
		    	
		    	public int getCount() {
		    		return data.size();
		    	}
		    	 
		    	public String getItem(int position) {
		    		return data.get(position).toString();
		    	}
		    	 
		    	public long getItemId(int position) {
		    		return position;
		    	}
		    	 
		    	public View getView(final int position, View convertView, ViewGroup parent) {
		    		LayoutInflater inflater = context.getLayoutInflater();
		    		View item = inflater.inflate(R.layout.listrecientes, null);
		    		
		    		TextView txt = (TextView)item.findViewById(R.id.listRecientes);
		    		ImageView iv = (ImageView)item.findViewById(R.id.companylogo);
		    		iv.setImageResource(identificarCompania(data.get(position).getNumero().toString()));
		    		
		    		if(data.get(position).getNombre().toString().length()==0)
		    			txt.setText(data.get(position).getNumero().toString());
					else
						txt.setText(data.get(position).getNombre().toString()+"\n"+data.get(position).getNumero().toString());
		    		
		    		
		    		
		    		
		    		return item;
		    	}
		    	 
		    }
		    	 
		 
		 
		 @Override
	        public boolean onKeyDown(int keyCode, KeyEvent event){   
	        	
	        	if(keyCode == KeyEvent.KEYCODE_BACK){
	        		onClickRegresar(null);
	        		return true;
	        	}
	    		return super.onKeyDown(keyCode, event);
	        }
		    	
		 
		    	
		 public int identificarCompania(String number){	    	
	    	   	int n=0;
				String num = number.substring(0,4);
				n = Integer.parseInt(num);
				
				if(((n>=7000)&&(n<=7099))||		     
				     ((n>=7600)&&(n<=7669))||
				     ((n>=7687)&&(n<=7690))||
				     ((n>=7740)&&(n<=7759))||
				     ((n>=7805)&&(n<=7809))||
				     ((n>=7840)&&(n<=7844))||
				     ((n>=7850)&&(n<=7869))||
				     ((n>=7950)&&(n<=7969))||
				     ((n>=7985)&&(n<=7989))){
						return CLARO;		
				}
									
				if(((n>=7300)&&(n<=7399))||		    			   
					((n>=7400)&&(n<=7449))||			   						   
					((n>=7460)&&(n<=7469))||
					((n>=7695)&&(n<=7698))||
					((n>=7760)&&(n<=7779))||
					((n>=7970)&&(n<=7979))){
					 return DIGICEL;			 
				}
			
				if(((n>=7100)&&(n<=7199))||
					  ((n>=7450)&&(n<=7459))||
					  ((n>=7691)&&(n<=7694))||
				      ((n>=7700)&&(n<=7719))||
				      ((n>=7780)&&(n<=7784))||
				      ((n>=7790)&&(n<=7794))||
				      ((n>=7810)&&(n<=7839))||
				      ((n>=7845)&&(n<=7849))||
				      ((n>=7990)&&(n<=7999))){
					return MOVISTAR;
				}
				
				if(((n>=7200)&&(n<=7299))||
				     ((n>=7470)&&(n<=7499))||
				     ((n>=7500)&&(n<=7599))||			     
				     ((n>=7670)&&(n<=7679))||
				     ((n>=7680)&&(n<=7683))||
				     ((n>=7720)&&(n<=7739))||
				     ((n>=7785)&&(n<=7789))||
				     ((n>=7795)&&(n<=7799))||
				     ((n>=7870)&&(n<=7899))||
				     ((n>=7900)&&(n<=7949))){
						return TIGO;

					}
				return -1;		    	
	    	}

}
